package com.aixingfu.sports;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.aixingfu.sports.MVP.contract.LoginContract;
import com.aixingfu.sports.MVP.presenter.LoginPresenter;
import com.aixingfu.sports.Model.LoginModel;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.orie.librarybase.ShardePreferencesConfig;
import com.orie.librarybase.base.BaseActivity;
import com.vondear.rxtool.RxSPTool;
import com.vondear.rxtool.view.RxToast;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//登录
@Route(path = "/main/LoginActivity")
public class LoginActivity extends BaseActivity implements LoginContract.MyView {

    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.tv_forget_pwd)
    TextView tvForgetPwd;
    @BindView(R.id.ed_phone)
    EditText edPhone;
    @BindView(R.id.ed_pwd)
    EditText edPwd;
    private LoginPresenter loginPresenter;

    @Override
    protected int provideContentViewId() {
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
                WindowManager.LayoutParams. FLAG_FULLSCREEN);
        return R.layout.activity_login;
    }

    public static void startActivityByRoute() {
        ARouter.getInstance().build("/main/LoginActivity").navigation();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        loginPresenter = new LoginPresenter();
        loginPresenter.attachView(this);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void beforeInit() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_login, R.id.tv_forget_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
                String phone = edPhone.getText().toString();
                String pwd = edPwd.getText().toString();
                if (TextUtils.isEmpty(phone)){
                    RxToast.normal(LoginActivity.this,"请输入账号").show();
                }else if (TextUtils.isEmpty(pwd)){
                    RxToast.normal(LoginActivity.this,"请输入密码").show();
                }else {
                    hideIputKeyboard();
                    getLoginData(phone,pwd);
                }
                break;
            case R.id.tv_forget_pwd:
                hideIputKeyboard();
                ForgetPasswordActivity.startActivityByRoute();
                break;
        }
    }
    public void getLoginData(String phone,String pwd){
        if (loginPresenter.isViewAttached()){
            HashMap<String,String> map = new HashMap<>();
            map.put("mobile",phone);
            map.put("password",pwd);
            loginPresenter.getLogin(map);
        }
    }
    @Override
    public void showToast(String msg, String state) {
    }

    @Override
    public void setLoginData(LoginModel data) {
        if (data.getCode() == 1){
            try {
                RxSPTool.putString(LoginActivity.this, ShardePreferencesConfig.ACCESSTOKEN, data.getData().getAccesstoken());//存储类型
                RxSPTool.putString(LoginActivity.this, ShardePreferencesConfig.USERNAME, data.getData().getEmployee_name());//存储姓名
                RxSPTool.putString(LoginActivity.this, ShardePreferencesConfig.USERID, String.valueOf(data.getData().getVenue_id()));//场馆ID
                RxSPTool.putString(LoginActivity.this, ShardePreferencesConfig.EMPLOYEEID, String.valueOf(data.getData().getEmployee_id()));//场馆ID
                RxSPTool.putString(LoginActivity.this, ShardePreferencesConfig.ORGANIZATION, String.valueOf(data.getData().getOrganization()));//用户权限
                RxSPTool.putString(LoginActivity.this, ShardePreferencesConfig.COMPANYID, String.valueOf(data.getData().getCompany_id()));//用户权限
                MainActivity.startActivityByRoute();
                finish();
            }catch (Exception e){
                e.printStackTrace();
            }
        }else {
            RxToast.normal(LoginActivity.this,data.getMessage()).show();

        }
    }


    @Override
    public void showLoading() {
        showLoadingDialog("登录中..");
    }

    @Override
    public void hideLoading() {
        dismissLoadingDialog();
    }

    @Override
    public Context getMyContext() {
        return this;
    }
    //关闭软键盘
    public void hideIputKeyboard() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                InputMethodManager mInputKeyBoard = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                if (getCurrentFocus() != null) {
                    mInputKeyBoard.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
                }
            }
        });
    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK ) {
            //do something.
            return true;
        }else {
            return super.dispatchKeyEvent(event);
        }
    }
}
